package com.accp.action;

import com.accp.biz.ComputerBiz;
import com.accp.dao.ComputerDao;
import com.accp.pojo.Computer;
import com.accp.pojo.Users;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SuppressWarnings("serial")
public class ComputerAction implements ModelDriven<Computer> {

    private Computer computer =new Computer();
    @Override
    public Computer getModel() {
        return computer;
    }

    private  ComputerBiz computerBiz;

    public void setComputerBiz(ComputerBiz computerBiz) {
        this.computerBiz = computerBiz;
    }

    public String selectFirstPage(){
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        try{
            Map<String,Object> session = ActionContext.getContext().getSession();
            List list = computerBiz.selectByPage("Computer", 1, 3);
            int totalPage = computerBiz.searchRecordsNextPage(3, "Computer");
            request.put("list", list);
            request.put("totalPage", totalPage);
            request.put("currentPage", 1);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "selectFirstPage";
    }
    /**
     * 分页查找下一页
     * @return
     */

    public String selectNextPage(){
        HttpServletRequest request = ServletActionContext.getRequest();
        int next = Integer.parseInt(request.getParameter("page"));
        try{
            Map<String,Object> session = ActionContext.getContext().getSession();
            List list = computerBiz.selectByPage("Computer", next, 3);
            int total = computerBiz.searchRecordsNextPage(3, "Computer");
            request.setAttribute("list", list);
            request.setAttribute("currentPage", next);
            request.setAttribute("totalPage", total);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "selectNextPage";
    }

    @SuppressWarnings("unchecked")
    public String showdetail(){
        Map<String,Object> request = (Map<String,Object>)ActionContext.getContext().get("request");
        int id = Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
        Computer computer = computerBiz.selectById(id);
        if(null!=computer){
            request.put("computer", computer);
            return "showdetail";
        }
        return "error";
    }

    public String delete(){
        HttpServletRequest request = ServletActionContext.getRequest();
        //获取页面传递的医生id
        String requestId = request.getParameter("id");
        int id = Integer.parseInt(requestId);
        //调用根据id查找实体方法
        Computer computer = computerBiz.selectById(id);
        if(computerBiz.delectComputer(computer)){
            return "delete";
        }else{
            return "error";
        }
    }

    public String add(){
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        Map<String,Object> session =  ActionContext.getContext().getSession();
        Users user = (Users) session.get("user");
        String userName = user.getUserName();
        computer.setCreatetime(new Date());
        computer.setCreateuser(userName);
        boolean flag = computerBiz.add(computer);
        if(flag){
            request.put("message", "添加成功");
            return "add";
        }
        return "error";
    }
    public String update(){
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        Computer computers=computerBiz.selectById(computer.getId());
        computers.setName(computer.getName());
        computers.setCategoryid(computer.getCategoryid());
        computers.setTitle(computer.getTitle());
        boolean flag = computerBiz.update(computer);
        if(flag){
            request.put("message", "修改成功");
            return "add";
        }
        return "error";
    }
}
